local shidao = fk.CreateSkill{
  name = "xiaobai__shidao",
  dynamic_desc = function (self, player, lang)
    local previous = #player:getTableMark("xiaobai__sanfa_previous")
    local next = #player:getTableMark("xiaobai__sanfa_next")
    local prompt = "xiaobai__shidao_dynamics:"
    if previous == 1 then
      prompt = prompt.."xiaobai__sanfa_slash:"
    else
      prompt = prompt.."xiaobai__sanfa_imediate:"
    end
    if next == 1 then
      prompt = prompt.."xiaobai__sanfa_electSlash"
    else
      prompt = prompt.."xiaobai__sanfa_allSlash"
    end
    return prompt
  end
}

local U = require "packages.utility.utility"

Fk:loadTranslationTable{
  ["xiaobai__shidao"] = "释道",
  [":xiaobai__shidao"] = "每回合限一次，一名角色受到属性伤害后，你可以重铸一张牌，若此牌是“三法”："..
    "牌名，你摸一张牌；底牌，你对一名角色造成1点相同属性伤害。",
  
  [":xiaobai__shidao_dynamics"] = "每回合限一次，一名角色受到属性伤害后，你可以重铸一张牌，若此牌为："..
    "{1}，你摸一张牌；{2}，你对一名角色造成1点相同属性伤害。",

  ["#xiaobai__shidao_recast"] = "释道：你可以重铸一张牌，若为%arg则你摸一张牌，若为%arg2你对一名角色造成一点%arg3伤害",
  ["#xiaobai__shidao_damge"] = "释道：你可以对一名角色造成1点%arg伤害"
}

shidao:addEffect(fk.Damaged,{
  can_trigger = function (self, event, target, player, data)
    return player:hasSkill(self) and player:usedSkillTimes(shidao.name) < 1 and data.damageType ~= fk.NormalDamage
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    local prompt = "#xiaobai__shidao_recast"
    local previous = player:getTableMark("xiaobai__sanfa_previous")
    local next = player:getTableMark("xiaobai__sanfa_next")
    if #previous == 1 then
      prompt = prompt..":::xiaobai__sanfa_slash:"
    else
      prompt = prompt..":::xiaobai__sanfa_imediate:"
    end

    if #next == 1 then
      prompt = prompt.."xiaobai__sanfa_electSlash:"
    else
      prompt = prompt.."xiaobai__sanfa_allSlash:"
    end

    prompt = prompt..Fk:getDamageNature(data.damageType)[1]

    local cards = room:askToCards(player,{
      min_num = 1,
      max_num = 1,
      skill_name = shidao.name,
      include_equip = true,
      prompt = prompt
    })
    if #cards > 0 then
      event:setCostData(self, cards)
      return true
    end
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local cards = event:getCostData(self) ---@type integer[]
    room:recastCard(cards, player, shidao.name)
    if player.dead then return end

    local previous = player:getTableMark("xiaobai__sanfa_previous")
    local next = player:getTableMark("xiaobai__sanfa_next")

    if table.contains(previous, Fk:getCardById(cards[1]).trueName) then
      room:setPlayerMark(player, "xiaobai__shidao_previous", 1)
      player:drawCards(1, shidao.name)
      if player.dead then return end
      room:delay(100)
    end

    if table.contains(next, Fk:getCardById(cards[1]).name) then
      room:setPlayerMark(player, "xiaobai__shidao_next", 1)
      local to = room:askToChoosePlayers(player, {
        min_num = 1,
        max_num = 1,
        skill_name = shidao.name,
        targets = room:getAlivePlayers(),
        cancelable = false,
        prompt = "#xiaobai__shidao_damge:::"..Fk:getDamageNature(data.damageType)[1]
      })
      room:damage{
        from = player,
        to = to[1],
        damage = 1,
        damageType = data.damageType
      }
    end
  end
})

return shidao